package com.tongjie.hotelbooking.controller.admin;

import com.tongjie.hotelbooking.dto.OrderDto;
import com.tongjie.hotelbooking.order.entity.Order;
import com.tongjie.hotelbooking.order.service.OrderService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {

    private static String DATE_PATTERN = "yyyy-MM-dd";

    private static String TIME_PATTERN = "HH:mm";

    @Resource
    private OrderService orderService;

    @GetMapping
    public Page<OrderDto> find(OrderDto orderDto, Pageable pageable) {
        Order order = Optional.ofNullable(orderDto).map(input -> Order.builder()
                .id(orderDto.getId())
                .orderNumber(orderDto.getOrderNumber())
                .orderType(orderDto.getOrderType())
                .region(orderDto.getRegion())
                .hotelName(orderDto.getHotelName())
                .dayCount(orderDto.getDayCount())
                .singleRoomBudget(orderDto.getSingleRoomBudget())
                .totalPeopleCount(orderDto.getTotalPeopleCount())
                .kidCount(orderDto.getKidCount())
                .needBreakfast(orderDto.getNeedBreakfast())
                .noSmokingBreakfast(orderDto.getNoSmoking())
                .additionalBudget(orderDto.getAdditionalBudget()).build()
        ).get();
        return this.orderService.findOrders(Example.of(order), pageable).map(input -> OrderDto.builder()
                .id(input.getId())
                .orderNumber(input.getOrderNumber())
                .orderType(input.getOrderType())
                .region(input.getRegion())
                .hotelName(input.getHotelName())
                .plannedCheckInDate(DateFormatUtils.format(input.getPlannedCheckInDate(), DATE_PATTERN))
                .plannedCheckOutDate(DateFormatUtils.format(input.getPlannedCheckOutDate(), DATE_PATTERN))
                .dayCount(input.getDayCount())
                .singleRoomBudget(input.getSingleRoomBudget())
                .totalPeopleCount(input.getTotalPeopleCount())
                .kidCount(input.getKidCount())
                .needBreakfast(input.getNeedBreakfast())
                .noSmoking(input.getNoSmokingBreakfast())
                .arriveTime(DateFormatUtils.format(input.getArriveTime(), TIME_PATTERN))
                .additionalBudget(input.getAdditionalBudget())
                .needHotelInvoice(input.getNeedHotelInvoice())
                .totalBudget(input.getTotalBudget())
                .allowedHousemate(input.getAllowedHousemate())
                .orderStatus(input.getOrderStatus())
                .creatorNickName(input.getCreateUser().getNickName())
                .createTime(DateFormatUtils.format(input.getCreateTime(), DATE_PATTERN))
                .build());
    }

}
